package org.prx.prp.utility;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class LocationHelper {

	public static final int FOUND_LOCATION = 140002;
	private static LocationManager locationManager;
	private static Handler sHandler;
	
	private static LocationListener locationListener = new LocationListener(){
		@Override
		public void onLocationChanged(Location location){
			locationManager.removeUpdates(this);
			Message.obtain(sHandler, FOUND_LOCATION, location).sendToTarget();
		}
		
		@Override
		public void onProviderDisabled(String arg0) {}

		@Override
		public void onProviderEnabled(String provider) {}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {}
	};

	public static boolean getLocation(Context context, Handler handler) {
		sHandler = handler;
		locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		String[] providers = {LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER};
		
		boolean isListening = false;
	    for (String provider : providers) {  
	      if(locationManager.isProviderEnabled(provider)){
	    	  locationManager.requestLocationUpdates(provider, 3000, 0f, locationListener);
	    	  isListening = true;
	      }
	    }
	    return isListening;
		
	}
}
